package com.gingko.duominuo.viewmodel

import android.widget.Toast
import androidx.hilt.Assisted
import androidx.hilt.lifecycle.ViewModelInject
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.SavedStateHandle
import com.gingko.duominuo.data.model.JobWorkVo
import com.gingko.duominuo.data.request.ReviewInfoVo
import com.gingko.duominuo.data.source.repository.GinkgoRepository
import com.gingko.duominuo.network.download.OSSUpload
import com.gingko.duominuo.network.download.UploadCallback
import com.gingko.duominuo.utils.toast
import kotlinx.coroutines.runBlocking
import java.io.File
import java.util.*

class ReviewWorkOrderViewModel @ViewModelInject constructor(
    private val ginkgoRepository: GinkgoRepository,
    @Assisted val state: SavedStateHandle
) : BaseViewModel() {
    private val _messageLiveData = MutableLiveData<String?>()
    val messageLiveData = _messageLiveData

    fun uploadImage(jobWorkVo: JobWorkVo, image: File) {
        _loadingLiveData.postValue(true)
        OSSUpload.uploadFile(image, object : UploadCallback {
            override fun onSuccess(url: String) {
                _loadingLiveData.postValue(false)
                jobWorkVo.images.add(url)
                insertJobWork(jobWorkVo)
                messageLiveData.postValue("保存成功")
            }

            override fun onFailure(message: String?) {
                _loadingLiveData.postValue(false)
                messageLiveData.postValue(message)
            }
        })
    }

    fun insertJobWork(jobWorkVo: JobWorkVo): List<Long> {
        return runBlocking {
            ginkgoRepository.insertJobWork(jobWorkVo)
        }
    }

    fun queryJobWork(orderId: String, jobId: String): JobWorkVo? {
        return runBlocking {
            ginkgoRepository.queryJobWork(orderId, jobId)
        }
    }

    fun queryJobWork(workId: String): List<JobWorkVo>? {
        return runBlocking {
            ginkgoRepository.queryJobWork(workId)
        }
    }

}